package com.dameng.jianyouquan;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.dameng.jianyouquan.api.Constant;
import com.dameng.jianyouquan.bean.LoginBean;
import com.dameng.jianyouquan.api.NetWorkManager;
import com.dameng.jianyouquan.interviewer.activity.MainInterViewerActivity;
import com.dameng.jianyouquan.jobhunter.activity.MainJobHunterActivity;
import com.dameng.jianyouquan.mvp.view.activity.ChooseIdentityActivity;
import com.dameng.jianyouquan.mvp.view.activity.LoginActivity;
import com.dameng.jianyouquan.utils.EdittextUtils;
import com.dameng.jianyouquan.utils.SpUtils;
import com.tbruyelle.rxpermissions.RxPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class SplashActivity extends AppCompatActivity {

    @BindView(R.id.iv_splash)
    ImageView ivSplash;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        Glide.with(this).load(R.mipmap.splash).into(ivSplash);


        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions
                .request(Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(granted -> {
                    if (granted) {
                        skip();
                    } else {
                        exit();
                    }
                });
    }

    private void skip() {
        new Handler().postDelayed(() -> {
            /**
             * 0未登录   1已登录 未选择角色   2已登录选择过角色
             */
            int status = SpUtils.getValueInt(getApplicationContext(), Constant.SP_STATUS);
            if (status == 0) {
                startActivity(new Intent(SplashActivity.this, LoginActivity.class));
            } else if (status == 1) {
                startActivity(new Intent(SplashActivity.this, ChooseIdentityActivity.class));
            } else if (status == 2) {
                String phoneNum = SpUtils.getValue(getApplicationContext(), Constant.SP_USERNAME);
                String pwd = SpUtils.getValue(getApplicationContext(), Constant.SP_USERPWD);
                if (TextUtils.isEmpty(phoneNum) || TextUtils.isEmpty(pwd)) {
                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                } else if (!EdittextUtils.isMobileNO(phoneNum)) {
                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                } else if (!EdittextUtils.isPWD(pwd)) {
                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                } else {
                    NetWorkManager.getService()
                            .getLogin(phoneNum, pwd, "")
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Observer<LoginBean>() {
                                @Override
                                public void onCompleted() {

                                }

                                @Override
                                public void onError(Throwable e) {
                                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                                }

                                @Override
                                public void onNext(LoginBean loginBean) {
                                    int code = loginBean.getCode();
                                    if (code == 200) {
                                        LoginBean.DataBean data = loginBean.getData();
                                        SpUtils.setValue(getApplicationContext(), Constant.SP_UID, data.getUserId());
                                        int identity = SpUtils.getValueInt(getApplicationContext(), Constant.SP_ROLE_ID);
                                        if (identity == 1) {
                                            startActivity(new Intent(SplashActivity.this, MainJobHunterActivity.class));
                                        } else if (identity == 2) {

                                        } else if (identity == 3) {
                                            startActivity(new Intent(SplashActivity.this, MainInterViewerActivity.class));
                                        }else {
                                            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                                        }
                                    } else {
                                        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                                    }
                                }
                            });
                }
            }
            finish();
        }, 2000);
    }

    private void exit() {
        Toast.makeText(getApplicationContext(), "缺少必要的权限", Toast.LENGTH_SHORT).show();
    }

}
